package com.supms.course.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.util.StringUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.supms.course.domain.TeachPlanSituation;
import com.supms.course.mapper.TeachPlanRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supms.course.mapper.TeachPlanItemsMapper;
import com.supms.course.domain.TeachPlanItems;
import com.supms.course.service.ITeachPlanItemsService;

/**
 * 教案检查评价Service业务层处理
 * 
 * @author LiangHao
 * @date 2022-03-03
 */
@Service
public class TeachPlanItemsServiceImpl implements ITeachPlanItemsService 
{
    @Autowired
    private TeachPlanItemsMapper teachPlanItemsMapper;

    @Autowired
    private TeachPlanRecordMapper teachPlanRecordMapper;

    /**
     * 查询教案检查评价
     * 
     * @param id 教案检查评价主键
     * @return 教案检查评价
     */
    @Override
    public TeachPlanItems selectTeachPlanItemsById(Long id)
    {
        return teachPlanItemsMapper.selectTeachPlanItemsById(id);
    }

    /**
     * 查询教案检查评价列表
     * 
     * @param teachPlanItems 教案检查评价
     * @return 教案检查评价
     */
    @Override
    public List<TeachPlanItems> selectTeachPlanItemsList(TeachPlanItems teachPlanItems)
    {
        return teachPlanItemsMapper.selectTeachPlanItemsList(teachPlanItems);
    }

    /**
     * 新增教案检查评价
     * 
     * @param teachPlanItems 教案检查评价
     * @return 结果
     */
    @Override
    public int insertTeachPlanItems(TeachPlanItems teachPlanItems)
    {
        teachPlanItems.setCreateTime(DateUtils.getNowDate());
        return teachPlanItemsMapper.insertTeachPlanItems(teachPlanItems);
    }

    /**
     * 修改教案检查评价
     * 
     * @param teachPlanItems 教案检查评价
     * @return 结果
     */
    @Override
    public int updateTeachPlanItems(TeachPlanItems teachPlanItems)
    {
        teachPlanItems.setUpdateTime(DateUtils.getNowDate());
        return teachPlanItemsMapper.updateTeachPlanItems(teachPlanItems);
    }

    /**
     * 批量删除教案检查评价
     * 
     * @param ids 需要删除的教案检查评价主键
     * @return 结果
     */
    @Override
    public int deleteTeachPlanItemsByIds(Long[] ids)
    {
        return teachPlanItemsMapper.deleteTeachPlanItemsByIds(ids);
    }

    /**
     * 删除教案检查评价信息
     * 
     * @param id 教案检查评价主键
     * @return 结果
     */
    @Override
    public int deleteTeachPlanItemsById(Long id)
    {
        return teachPlanItemsMapper.deleteTeachPlanItemsById(id);
    }

    /**
     * 获取当前课程类型的教案检查评价项列表
     *
     * @param typeNum 课程类型编号
     * @return 当前课程类型的教案检查评价项列表
     */
    @Override
    public List<TeachPlanItems> getTeachPlanItemByType(String typeNum) {
        return teachPlanItemsMapper.getTeachPlanItemByType(typeNum);
    }

    /**
     * 添加教案检查评价
     *
     * @param teachPlanItemsList 接收到的评分列表
     * @return 结果
     */
    @Override
    public int addTeachPlanEval(List<TeachPlanItems> teachPlanItemsList) {

        List<TeachPlanSituation> tpSituationList = new ArrayList<>();
        Long teachPlanRecordId = null;

        // 遍历提交的评分列表，将评分结果、教案检查记录表ID和评价项表ID添加到教案检查评价表对象中
        for (TeachPlanItems teachPlanItems : teachPlanItemsList) {
            // 判断是否未进行评价就进行提交，若已评价则将结果插入到中间表中
            if (StringUtil.isNotEmpty(teachPlanItems.getSelectOption())) {
                teachPlanRecordId = teachPlanItems.getTeachPlanRecordId();
                TeachPlanSituation tpSituation = new TeachPlanSituation();
                tpSituation.setTeachPlanRecordId(teachPlanItems.getTeachPlanRecordId());
                tpSituation.setTeachPlanItemsId(teachPlanItems.getId());
                tpSituation.setSituation(teachPlanItems.getSelectOption());
                tpSituation.setCreateTime(DateUtils.getNowDate());
                tpSituationList.add(tpSituation);
            } else return 0;
        }

        // 将教案检查记录表中的评价标记改为已评价标记
        teachPlanRecordMapper.updateTeachPlanSituationFlagById(teachPlanRecordId);
        return teachPlanItemsMapper.addTeachPlanEval(tpSituationList);
    }
}
